/*
 *  Copyright 2012 Iurii Dunko, Iana Potochniak.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.opu.yyminijava.codegeneration;

import org.opu.yyminijava.util.CodeExecuteException;
import org.opu.yyminijava.util.CodeGenerationException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.ElementMap;
import org.simpleframework.xml.Root;

/**
 *
 * @author Iurii Dunko
 * @author Iana Potochniak
 */
@Root
public class ClassFile extends Printable implements Serializable {

    /*
     * At the end of "our" simplifying we have:
     * classes can have just one method with the same name;
     * classes cannot have fields;
     */

    private static final long serialVersionUID = -7766233440686854981L;
	
    /** The name of the current class */
    @Attribute
    private String name;
    /** Set of methods of current class */
    @ElementMap
    private HashMap<String, Method> methods = new HashMap<String, Method>();

    public ClassFile(String name) {
        this.name = name;
    }
    
    /**@deprecated this constructor must be used only for serialization */
    public ClassFile() {
    }

    public Method addMethod(String name){
        Method method = new Method(this.name + '.' + name);
        addMethod(name, method);
        return method;
    }

    public void addMethod(String name, Method method) {
        if (methods.containsKey(name)) {
            throw new CodeGenerationException("Class " + this.name + " already have "
                    + "method " + name);
        }

        methods.put(name, method);
    }

    public Method getMethod(String name) {
        if (methods.containsKey(name)) {
            return methods.get(name);
        } else {
            throw new CodeExecuteException("Class " + this.name + " don't containe "
                    + "method " + name);
        }
    }

    public String getName() {
        return name;
    }

    /**
     * @deprecated this method must be used only for serialization
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @deprecated this method must be used only for serialization
     */
    public Map<String, Method> getMethods() {
        return methods;
    }

    /**
     * @deprecated this method must be used only for serialization
     */
    public void setMethods(HashMap<String, Method> methods) {
        this.methods = methods;
    }

    @Override
    public void print(String space) {
        out.println(space + name + "{");
        for (Method method : methods.values()) {
            method.print(space + "    ");
        }
        out.println(space + "}");
    }
}
